/*
 * vm_exec.h
 *
 *  Created on: 2009-12-2
 *      Author: Bearice
 */

#ifndef VM_EXEC_H_
#define VM_EXEC_H_
#include <vm.h>
#include <loader.h>

struct avm_pair {
	void* left;
	void* right;
};
struct avm_string{
	uint32 size;
	uint8* data;
};
struct avm_stack {
	uint32 size;
	uint32 idx;
	struct avm_object** ptr;
};
struct avm_heap {
	uint32 size;
	struct avm_object** ptr;
};
struct avm_function{
	struct avm_section* section;
	struct avm_heap* closure;
};
struct avm_object {
	uint32 tag;
	uint32 ref;
	struct avm_object** hashcode;
	union {
		uint32 uival;
		int32 ival;
		float fval;
		void* pval;
		uint64 ulval;
		int64 lval;
		double dval;
		struct avm_string str;
		struct avm_pair pair;
		struct avm_function fun;
	} val;
};

struct avm_frame {
	struct avm_frame* parent;
	struct avm_code* code;
	struct avm_section* code_section;
	struct avm_loader_status* loader;
	struct avm_stack* stack;
	struct avm_heap heap;
	struct avm_heap* closure;
};
extern struct avm_object* popObject(struct avm_frame* frame);
extern struct avm_object* pushObject(struct avm_frame* frame,struct avm_object* obj);
extern uint32 popUInt32(struct avm_frame* frame);
extern uint32 pushUInt32(struct avm_frame* frame, uint32 val);
extern double popDouble(struct avm_frame* frame);
extern double pushDouble(struct avm_frame* frame, double val);
extern struct avm_object* frame_heap_get(struct avm_frame* fr,uint32 idx);
extern struct avm_object* frame_heap_set(struct avm_frame* fr,uint32 idx,struct avm_object* obj);
extern struct avm_object* heap_get(struct avm_heap* heap,uint32 idx);
extern struct avm_object* heap_set(struct avm_heap* heap,uint32 idx,struct avm_object* obj);
extern struct avm_heap* heap_alloc(struct avm_heap* heap,uint32 size);
extern uint32 heap_free(struct avm_heap* heap);
extern struct avm_frame* frame_alloc(struct avm_frame* ref);
extern uint32 frame_free(struct avm_frame* frame);
extern struct avm_object* avm_load_constant(struct avm_frame* frame,uint32 idx);
extern uint32 avm_equals(struct avm_object* o1, struct avm_object* o2);
extern struct avm_frame* avm_step(struct avm_frame* frame);
#endif /* VM_EXEC_H_ */
